/*
 * @lc app=leetcode.cn id=1027 lang=javascript
 *
 * [1027] 最长等差数列
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var longestArithSeqLength = function (nums) {
  const n = nums.length;
  // 以 nums[i] 结尾，以 d 为差值存入字典的的最长等差数列长度
  const lengths = Array.from({ length: n }, () => new Map());
  let maxLen = 2;
  for (let i = 1; i < n; i++) {
    for (let j = 0; j < i; j++) {
      const d = nums[j] - nums[i];
      lengths[i].set(d, lengths[j].get(d) ? lengths[j].get(d) + 1 : 2);
      maxLen = Math.max(lengths[i].get(d), maxLen);
    }
  }
  return maxLen;
};
// @lc code=end
